layui.use(['form', 'layer'], function(){
    var $ = layui.jquery;
    var form = layui.form;
    var layer = layui.layer;

    var countdown = 180;
    var timer = null;
    window.sendSmsCode = function() {
        var $btn = $('.reg');
        $btn.attr('disabled', true);
        countdown = 180;
        timer = setInterval(function(){
            if(countdown > 0){
                $btn.text(`${countdown}秒后重试`);
                countdown--;
            }else{
                clearInterval(timer);
                $btn.removeAttr('disabled').text('获取验证码');
            }
        }, 1000);
        var username = $(`input[name="user"]`).val();
        var qq = $(`input[name="qq"]`).val();
        var email = $(`input[name="email"]`).val();
        var captcha = $(`input[name="img_code"]`).val();
        $.ajax({
            url: './ajax/send_reg_code',
            type: 'POST',
            data: {
                user: username,
                qq: qq,
                email:email,
                captcha:captcha
            },
            dataType: 'json',
            success: function(res){
                if(res.code == 200){
                    layer.msg(res.msg, {icon: 1});
                }else{
                    layer.msg(res.msg || '发送失败', {icon: 2});
                    clearInterval(timer);
                    $btn.removeAttr('disabled').text('获取验证码');
                    refreshCaptcha();
                }
            },
            error: function(){
                layer.msg('服务器错误', {icon: 2});
                clearInterval(timer);
                $btn.removeAttr('disabled').text('获取验证码');
                refreshCaptcha();
            }
        });
    }

    form.on('submit(reg)', function (data) {
        var ii = layer.load(2, {shade:[0.1,'#fff']});
        $.ajax({
            type : "POST",
            url : "./ajax/reg",
            data: JSON.stringify(data.field),
            contentType: 'application/json',
            dataType : 'json',
            success : function(data){
                layer.close(ii);
                if(data.code == 200){
                    truemsg(layer , data.msg,function(){
                        location.href="./";
                    });
                } else if (data.code == 1) {
                    layer.open({
                        type: 1,
                        title: '<i class="layui-icon layui-icon-rmb"></i> 选择支付方式',
                        area: ['400px', '300px'],
                        shadeClose: true,
                        skin: 'custom-layer-skin', // 使用自定义的 Skin
                        content: generateContent(data),
                        btn: false,
                        success: function(layero, index) {
                          $(layero).find('.pay-button').on('click', function() { // 修改选择器
                            var payType = $(this).data('type');
                            handlePay(payType, data.token, index);
                          });
                        }
                      });
                    
                      function generateContent(data) {
                        let content = `<div class="payment-container">
                                          <p class="payment-amount">支付金额：<span class="amount">${data.money}</span></p>
                                          <div class="payment-options">`;
                    
                        for (let key in data.pay_type) {
                          content += `<button class="pay-button" data-type="${key}">
                                        <img src="/assets/icon/${key}.ico" alt="${data.pay_type[key]}">
                                        ${data.pay_type[key]}
                                      </button>`;
                        }
                    
                        content += `</div></div>`;
                        return content;
                      }
                    
                      function handlePay(type, token, index) {
                        $.ajax({
                          type: "POST",
                          url: "./ajax/reg_pay",
                          data: {
                            type: type,
                            token: token,
                          },
                          dataType: 'json',
                          success: function(res) {
                            if (res.code == 200) {
                              layer.close(index);
                              layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                              }, function() {
                                window.location.href = res.pay_url;
                              });
                            } else {
                              layer.msg(res.msg || '拉起支付失败', {
                                icon: 2
                              });
                            }
                          },
                          error: function() {
                            falsemsg(layer, "服务器错误！");
                          }
                        });
                      }
                }
                else{
                    falsemsg(layer , data.msg);
                    refreshCaptcha();
                }
            },
            error: function(data){
                layer.close(ii);
                falsemsg(layer , "服务器错误！");
                refreshCaptcha();
            }
        });

        return false;
    });
});
